﻿using System.IO;
using System.Windows.Media.Imaging;
using Gamelight.Graphics;

namespace Gamelight.Data
{
	public class WebImageLoader : WebLoader
	{
		private Image finalImage = null;

		public WebImageLoader(string url) : base(url) { }

		public WebImageLoader(string url, int maxAttempts, int millisecondsBetweenAttempts) : base(url, maxAttempts, millisecondsBetweenAttempts) { }

		public Image Image { get { return this.finalImage; } }

		protected override void ApplyStreamResult(Stream stream)
		{
			BitmapImage bmp = new BitmapImage();
			bmp.SetSource(stream);
			WriteableBitmap wbmp = new WriteableBitmap(bmp);

			int width = wbmp.PixelWidth;
			int height = wbmp.PixelHeight;

			Image finalImage = new Image(width, height);
			wbmp.Pixels.CopyTo(finalImage.Pixels, 0);

			this.finalImage = finalImage;
			this.complete = true;
		}
	}
}